<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookie记录网页访问次数</title>
</head>
<body>
 <%
    //先找
    Cookie[] cr = request.getCookies();//获取所有Cookie
    int visitNum=0;  
    for(int i=0;cr!=null&&i<cr.length;i++){
    	if(cr[i].getName().equals("visit")){
    		visitNum=Integer.parseInt(cr[i].getValue());
    		break;
    	}
    }
    visitNum++;
    Cookie c=new Cookie("visit",String.valueOf(visitNum));
    c.setPath("/");
    c.setMaxAge(864000);
    response.addCookie(c);
 %>
 <h2>您已经访问了<%=visitNum%>次</h2>
</body>
</html>